package com.xiaoyao.novel.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xiaoyao.novel.dao.ChapterDao;
import com.xiaoyao.novel.pojo.Chapter;

public class ChapterService extends BaseService<Chapter> {

	static ChapterDao dao = new ChapterDao();

	public ChapterService() {
		super(dao);
	}
	/**
	 * 第一个,前一张的ID,第二个后一章的ID
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	public List<Integer> getPrevNextChapter(int bookId, int chapterId) {
		List<Integer> idList =new ArrayList<Integer>();
		Map<String, Integer> map = new HashMap<String, Integer>();
		try {
			map.put("bookId", bookId);
			map.put("cid", chapterId);
			idList = dao.getPrevNextChapter( map);
		} catch (Exception e) {
			// e.printStackTrace();
			logger.error(e);
		}
		return idList;
	}

	public int getNextChapter(int bookId, int chapterId) {
		int nextId = 0;
		Map<String, Integer> map = new HashMap<String, Integer>();
		try {
			map.put("bookId", bookId);
			map.put("cid", chapterId);
			nextId = dao.getNextChapter(map);
		} catch (Exception e) {
			// e.printStackTrace();
			logger.error(e);
		}
		return nextId;
	}

	public int getPrevChapter(int bookId, int chapterId) {
		int prevId = 0;
		Map<String, Integer> map = new HashMap<String, Integer>();
		try {
			map.put("bookId", bookId);
			map.put("cid", chapterId);
			prevId = dao.getPrevChapter(map);
		} catch (Exception e) {
			// e.printStackTrace();
			logger.error(e);
		}
		return prevId;
	}

	public static void main(String[] args) {
		ChapterService chapterService = new ChapterService();
		chapterService.insert(new Chapter());
	}

	public void deleteBook(int bookId) {
		try {
			dao.deleteBook(bookId);
		} catch (SQLException e) {
			logger.error(e);
		}
	}

}
